package com.ycy.smartcourseselection.utils;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ycy.smartcourseselection.dto.course.FindAllCourseDTO;
import com.ycy.smartcourseselection.dto.score.FindAllScoreDTO;
import com.ycy.smartcourseselection.dto.student.FindAllStuDTO;
import com.ycy.smartcourseselection.dto.teacher.FindAllTchDTO;
import com.ycy.smartcourseselection.entity.Course;
import com.ycy.smartcourseselection.entity.Score;
import com.ycy.smartcourseselection.entity.Student;
import com.ycy.smartcourseselection.entity.Teacher;

public class WrapperUtils {

    public static QueryWrapper<Course> buildAllCourseWrapper(FindAllCourseDTO otherParams,QueryWrapper<Course> courseWrapper) {
        String courseCode = otherParams.getCourseCode();
        String courseId = otherParams.getCourseId();
        String workId = otherParams.getWorkId();
        String courseName = otherParams.getCourseName();
        String teachClass = otherParams.getTeachClass();
        String weekDay = otherParams.getWeekDay();
        String orderNum = otherParams.getOrderNum();
        String week = otherParams.getWeek();
        Integer maxPerson = otherParams.getMaxPerson();
        String place = otherParams.getPlace();
        String courseType = otherParams.getCourseType();
        Double credit = otherParams.getCredit();
        String classHourType = otherParams.getClassHourType();
        String openCollege = otherParams.getOpenCollege();
        String allowBand = otherParams.getAllowBand();
        Integer totalHours = otherParams.getTotalHours();
        String grade = otherParams.getGrade();
        String stage = otherParams.getStage();
        String schoolYear = otherParams.getSchoolYear();
        Integer courseIsDelete = otherParams.getCourseIsDelete();


        // QueryWrapper<Course> courseWrapper = new QueryWrapper<>();
        courseWrapper.eq(StrUtil.isNotBlank(courseCode), "course_code", courseCode);
        courseWrapper.eq(StrUtil.isNotBlank(courseId), "id", courseId);
        courseWrapper.eq(StrUtil.isNotBlank(workId), "work_id", workId);
        courseWrapper.like(StrUtil.isNotBlank(courseName), "course_name", courseName);
        courseWrapper.like(StrUtil.isNotBlank(teachClass), "teach_class", teachClass);
        courseWrapper.like(StrUtil.isNotBlank(weekDay), "week_day", weekDay);
        courseWrapper.like(StrUtil.isNotBlank(orderNum), "order_num", orderNum);
        courseWrapper.like(StrUtil.isNotBlank(week), "week", week);
        courseWrapper.eq(maxPerson != null, "max_person", maxPerson);
        courseWrapper.like(StrUtil.isNotBlank(place), "place", place);
        courseWrapper.eq(StrUtil.isNotBlank(courseType), "course_type", courseType);
        courseWrapper.eq(credit != null, "credit", credit);
        courseWrapper.eq(StrUtil.isNotBlank(classHourType), "class_hour_type", classHourType);
        courseWrapper.like(StrUtil.isNotBlank(openCollege), "open_college", openCollege);
        courseWrapper.like(StrUtil.isNotBlank(allowBand), "allow_band", allowBand);
        courseWrapper.eq(totalHours != null, "total_hours", totalHours);
        courseWrapper.eq(StrUtil.isNotBlank(grade), "grade", grade);
        courseWrapper.eq(StrUtil.isNotBlank(stage), "stage", stage);
        courseWrapper.like(StrUtil.isNotBlank(schoolYear), "school_year", schoolYear);
        courseWrapper.eq(courseIsDelete != null, "is_delete", courseIsDelete);

        return courseWrapper;
    }

    public static QueryWrapper<Teacher> buildAllTchWrapper(FindAllTchDTO otherParams, QueryWrapper<Teacher> wrapper) {
        String workId = otherParams.getWorkId();
        String identityId = otherParams.getIdentityId();
        String name = otherParams.getName();
        Integer sex = otherParams.getSex();
        Integer isResigned = otherParams.getIsResigned();
        Integer isAdmin = otherParams.getIsAdmin();

        wrapper.eq(sex != null, "sex", sex);
        wrapper.eq(isResigned != null, "is_resigned", isResigned);
        wrapper.eq(isAdmin != null, "is_admin", isAdmin);
        wrapper.eq(StrUtil.isNotBlank(workId), "work_id", workId);
        wrapper.eq(StrUtil.isNotBlank(identityId), "identity_id", identityId);
        wrapper.like(StrUtil.isNotBlank(name), "name", name);
        return wrapper;
    }

    public static QueryWrapper<Student> buildAllStuWrapper(FindAllStuDTO otherParams, QueryWrapper<Student> wrapper) {
        String studentId = otherParams.getStudentId();
        String identityId = otherParams.getIdentityId();
        String name = otherParams.getName();
        Integer sex = otherParams.getSex();
        String stuClass = otherParams.getStuClass();
        String major = otherParams.getMajor();
        String college = otherParams.getCollege();
        Integer isDelete = otherParams.getIsDelete();
        String grade = otherParams.getGrade();

        wrapper.eq(isDelete != null,"is_delete",isDelete);
        wrapper.eq(sex != null, "sex", sex);
        wrapper.eq(StrUtil.isNotBlank(stuClass), "stu_class", stuClass);
        wrapper.eq(StrUtil.isNotBlank(grade), "grade", grade);
        wrapper.eq(StrUtil.isNotBlank(studentId), "student_id", studentId);
        wrapper.eq(StrUtil.isNotBlank(identityId), "identity_id", identityId);
        wrapper.like(StrUtil.isNotBlank(name), "name", name);
        wrapper.like(StrUtil.isNotBlank(major), "major", major);
        wrapper.like(StrUtil.isNotBlank(college), "college", college);
        return wrapper;
    }

    public static QueryWrapper<Score> buildAllScoreWrapper(FindAllScoreDTO otherParams, QueryWrapper<Score> wrapper) {
        String courseId = otherParams.getCourseId();
        String studentId = otherParams.getStudentId();
        String workId = otherParams.getWorkId();
        Double score = otherParams.getScore();

        wrapper.eq(StrUtil.isNotBlank(courseId), "course_id", courseId);
        wrapper.eq(StrUtil.isNotBlank(studentId), "student_id", studentId);
        wrapper.eq(StrUtil.isNotBlank(workId), "work_id", workId);
        wrapper.eq(score != null, "score", score);

        return wrapper;
    }
}
